package com.hbm.dao.impl;

import java.util.List;
import java.util.Scanner;

import org.junit.Test;

import com.hbm.Customer.service.Menu;
import com.hbm.entity.Customer;

public class systemImpl {
	static Scanner scanner = new Scanner(System.in);

	@Test
	public void method() {
		for (;;) {
			Menu.showMenu();
			System.out.println("请选择操作:");
			int nextInt = scanner.nextInt();
			if (nextInt == 1) {
				insert();
			} else if (nextInt == 2) {
				new CustomerDaoImpl().delectCustomer();
			} else if (nextInt == 3) {
				update();
			} else if (nextInt == 4) {
				search();
			} else if (nextInt == 5) {
				showall();
			} else if (nextInt == 0) {
				Menu.quit();
				break;
			} else {
				System.out.println("请重新选择操作!");
			}
		}
	}

	public static void insert() {
		System.out.println("请您输入编号：");
		long nextInt = scanner.nextInt();
		System.out.println("请您输入名字：");
		String nextLine = scanner.next();
		System.out.println("请您输入地址：");
		String nextLine2 = scanner.next();
		Customer customer = new Customer(nextInt, nextLine, nextLine2);
		new CustomerDaoImpl().insertCustomer(customer);

	}

	public static void update() {
		System.out.println("请您输入编号：");
		long nextInt = scanner.nextInt();
		System.out.println("请您输入名字：");
		String nextLine = scanner.next();
		System.out.println("请您输入地址：");
		String nextLine2 = scanner.next();
		Customer customer = new Customer(nextInt, nextLine, nextLine2);
		new CustomerDaoImpl().updateCustomer(customer);

	}

	public static void search() {
		for (;;) {
			Menu.subMenu();
			int id = scanner.nextInt();
			if (id == 1) {
				searchById();
			}
			if (id == 2) {
				searchByName();
			}
			if (id == 3) {
				searchByAddress();
			}
			if (id == 4) {
				break;
			}
		}
	}

	public static void searchById() {
		System.out.println("请您输入编号：");
		int nextInt = scanner.nextInt();
		Customer s = new CustomerDaoImpl().selectCustomerById((long) nextInt);
		if (s == null) {
			System.out.println("客户不存在!");
		} else {
			System.out.println("结果:" + "\t" + "编号" + "\t" + "名称" + "\t" + "地址" + "\t");
			System.out.println("\t" + s.getId() + "\t" + s.getName() + "\t" + s.getAddress());
		}

	}

	public static void searchByName() {
		System.out.println("请您输入姓名：");
		String next1 = scanner.next();
		Customer s = new CustomerDaoImpl().selectCustomerByName(next1);
		if (s == null) {
			System.out.println("客户不存在!");
		} else {
			System.out.println("结果:" + "\t" + "编号" + "\t" + "名称" + "\t" + "地址" + "\t");
			System.out.println("\t" + s.getId() + "\t" + s.getName() + "\t" + s.getAddress());
		}

	}

	public static void searchByAddress() {
		System.out.println("请您输入地址：");
		String next2 = scanner.next();
		Customer s = new CustomerDaoImpl().selectCustomerByAddress(next2);
		if (s == null) {
			System.out.println("客户不存在!");
		} else {
			System.out.println("结果:" + "\t" + "编号" + "\t" + "名称" + "\t" + "地址" + "\t");
			System.out.println("\t" + s.getId() + "\t" + s.getName() + "\t" + s.getAddress());
		}

	}

	public static void showall() {
		List<Customer> s = new CustomerDaoImpl().selectAllCustomers();
		System.out.println("结果:" + "\t" + "编号" + "\t" + "名称" + "\t" + "地址" + "\t");
		for (Customer customer : s) {
			System.out.println("\t" + customer.getId() + "\t" + customer.getName() + "\t" + customer.getAddress());
		}

	}

}
